//
//  Picview.m
//  UI_Homework_Day6_02
//
//  Created by 黄威 on 16/5/10.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "Picview.h"
#import "PictureScrollView.h"
@interface Picview ()<UIScrollViewDelegate>

@property (nonatomic ,retain)UIPageControl *page;

@property (nonatomic ,retain)PictureScrollView *picScroll;

@end


@implementation Picview



-(void)dealloc{

    [_page release];
    [_picScroll release];
    [super dealloc];


}

- (instancetype)initWithFrame:(CGRect)frame{


     self = [super initWithFrame:frame];
    
    if (self) {
        
        self.picScroll = [[PictureScrollView alloc]initWithFrame:self.bounds];
       
        
        [self addSubview:self.picScroll];
         [_picScroll release];
        
        self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 40, CGRectGetWidth(self.bounds), 40)];
        
        [self addSubview:self.page];
        
        self.picScroll.delegate = self;
        [_page release];
        
        [self.page addTarget:self action:@selector(handle:) forControlEvents:UIControlEventValueChanged];
        
        
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
        
    }
    

    return self;
}


- ( void)timeAction:(NSTimer *)time{

    int i = arc4random() % 12 ;
    [self.picScroll setContentOffset:CGPointMake(i *CGRectGetWidth(self.bounds),0 )];
    


}

- (void)handle:(UIPageControl *)page{

    [self.picScroll setContentOffset:CGPointMake(page.currentPage *CGRectGetWidth(self.bounds),0 )animated:YES];



}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{



    self.page.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
    self.page.currentPage = 1;
    
    
    if (scrollView.contentOffset.x /CGRectGetWidth(self.bounds) == 0) {
        
         [self.picScroll setContentOffset:CGPointMake(_page.currentPage *CGRectGetWidth(self.bounds),0 )animated:YES];
    } else if(scrollView.contentOffset.x /CGRectGetWidth(self.bounds) == 11){
    
    [self.picScroll setContentOffset:CGPointMake(_page.currentPage *CGRectGetWidth(self.bounds),0 )animated:YES];
    }


}

//
//- (void)timerFired:(NSTimer *)time{
//
//
//
//
//}
- (void)addpicture:(NSArray<UIImage *> *)picture{

    [self.picScroll addpicture:picture];
    
    self.page.numberOfPages = picture.count;


}



@end
